/**
 * @fileOverview 升级系统 (经验 等级)
 */

module.exports = {
    attributes: {
        exp: {
            label: '经验',
            type: 'datatype.Integer',
            options: {min: 0},
            defaultValue: 0,
            setter: function(v) {
                this.defaultSetter('exp', v);
                var oldValue = this._attributes['level'];
                var newValue = this.expToLevel(v);
                this.trigger('change:level', this, newValue, oldValue);
                this.trigger('change', this, 'level', newValue, oldValue);
            }
        },
        level: {
            label: '等级',
            type: 'datatype.Integer',
            options: {min: 0},
            defaultValue: 1
        }
    },
    methods: {
        /**
         * 默认的经验等级之间的升级公式
         */
        expToLevel: function(v) {
            if (this.dict && typeof this.dict['formulaExp'] === 'function') {
                return this.dict['formulaExp'].call(this, v);
            } else {
                return v;
            }
        },
        /**
         * 增加经验值
         * @param {Number} v
         */
        addExp: function(v) {
            var oldExp = this.get('exp');
            this.set('exp', oldExp + v);
            var oldLevel = this.get('level');
            var newLevel = this.expToLevel(v);
            //保证一级一级往上升
            for (var i = oldLevel + 1; i < newLevel; i++) {
                this.set('level', newLevel);
            }
        }
    },
    events: {}
};
